<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 分类信息管理 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <!-- <el-input v-model="query.id" placeholder="作者编号" class="handle-input mr10" style="width: 100px"></el-input>
                <el-select v-model="query.sex" placeholder="性别" class="handle-select mr10" clearable>
                    <el-option key="1" label="男" value="1"></el-option>
                    <el-option key="2" label="女" value="0"></el-option>
                </el-select>
                <el-input v-model="query.name" placeholder="作者姓名" class="handle-input mr10" style="width: 150px"></el-input> -->
                <el-button type="primary" icon="el-icon-circle-plus-outline" @click="addType">添加分类</el-button>
                <el-button type="primary" icon="el-icon-folder-add" @click="mergeType">合并分类</el-button>
            </div>
            <el-table :data="typeList" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="name" label="姓名" width="400"></el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope" v-if="scope.row.name != '默认'">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="deleteType(scope.$index, scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="type" label-width="70px" :rules="ruleForm">
                <el-form-item label="编号" prop="id">
                    <el-input v-model="type.id" disabled></el-input>
                </el-form-item>
                <el-form-item label="名称" prop="name">
                    <el-input v-model="type.name"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit('form')">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 新增弹出框 -->
        <el-dialog title="新增" :visible.sync="addVisible" width="30%">
            <el-form label-width="70px">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="newTypeName"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 合并弹出框 -->
        <el-dialog title="合并" :visible.sync="mergeVisible" width="30%">
            <el-form label-width="70px">
                <el-select v-model="retainType" placeholder="保留分类" class="handle-select mr10" clearable filterable>
                    <div v-for="(item, index) in typeList" :key="index">
                        <el-option
                            key="1"
                            :label="item.name"
                            :value="item.name"
                            v-if="(item.name != '默认') & (item.name != removeType)"
                        ></el-option>
                    </div>
                </el-select>
                <el-select v-model="removeType" placeholder="被合并分类" class="handle-select mr10" clearable filterable>
                    <div v-for="(item, index) in typeList" :key="index">
                        <el-option
                            key="1"
                            :label="item.name"
                            :value="item.name"
                            v-if="(item.name != '默认') & (item.name != retainType)"
                        ></el-option>
                    </div>
                </el-select>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="mergeVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveMerge">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 删除弹出框 -->
        <el-dialog title="提示" :visible.sync="deleteVisible" width="30%" center>
            <span>确定删除该分类吗</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="deleteVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveDelete">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: 'basetable',
    data() {
        return {
            typeList: [],
            type: {
                id: '',
                name: ''
            },
            newTypeName: '',
            ruleForm: {
                id: [],
                name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
            },
            editVisible: false,
            addVisible: false,
            mergeVisible: false,
            deleteVisible: false,
            retainType: '',
            removeType: '',
            deleteIndex:'',
            deleteTypeName:'',
            idx: -1
        };
    },
    created() {
        this.getTypeList();
    },
    methods: {
        //获取分类列表
        getTypeList() {
            this.$http.get('/type/list', this.query).then((res) => {
                this.typeList = res.data.data;
            });
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.type = JSON.parse(JSON.stringify(row));
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit(formName) {
            this.$refs[formName].validate((valid) => {
                this.$http.post('/type/update', this.type).then((res) => {
                    if (res.data.status == 1) {
                        this.$set(this.typeList, this.idx, this.type);
                        this.editVisible = false;
                        this.$message.success(`修改 ${this.type.id} 号分类信息成功`);
                        this.resetForm('form');
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },

        addType() {
            this.newTypeName = '';
            this.addVisible = true;
        },
        saveAdd() {
            if (this.newTypeName == '') {
                this.$message.error('分类名称不能为空');
            } else {
                this.$http.post('/type/add/' + this.newTypeName).then((res) => {
                    if (res.data.status == 1) {
                        console.log(res.data);
                        this.typeList = this.typeList.concat(res.data.data);
                        this.addVisible = false;
                        this.$message.success('分类新增成功');
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
            }
        },
        mergeType() {
            this.retainType = '';
            this.removeType = '';
            this.mergeVisible = true;
        },
        saveMerge() {
            if ((this.retainType == '') | (this.removeType == '')) {
                this.$message.error('分类不能为空');
            } else {
                this.$http.post('/type/merge/' + this.retainType + '/' + this.removeType).then((res) => {
                    if (res.data.status == 1) {
                        this.getTypeList();
                        this.mergeVisible = false;
                        this.$message.success('合并成功');
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
            }
        },
        deleteType(index, row){
            this.deleteTypeName = row.name;
            this.deleteIndex = index;
            this.deleteVisible = true;
        },
        saveDelete(){
            this.$http.post('/type/delete/'+this.deleteTypeName).then(res => {
                if(res.data.status==1){
                    this.typeList.splice(this.deleteIndex,1);
                    this.$message.success("分类"+this.deleteTypeName+"删除成功");
                    this.deleteVisible = false;
                }
            });
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
